import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:vibration/vibration.dart';

import 'utils_ui.dart';

class UtilsSystem {
  /// 深色模式
  static bool isDarkMode(BuildContext context) {
    return Theme.of(context).brightness == Brightness.dark;
  }

  /// 复制
  static Future<void> copy(text, {String tip = "复制成功"}) async{
    UIUtil.showToast(tip);
    await Clipboard.setData(ClipboardData(text: text));
  }

  /// 粘贴
  static Future<String?> paste() async {
    //读取剪切板
    ClipboardData? text = await Clipboard.getData(Clipboard.kTextPlain);
    return text?.text;
  }

  /// 权限申请
  static Future<PermissionStatus> requestPermission(Permission permission) async {
    PermissionStatus permissionStatus = await permission.status;
    PermissionStatus condition = PermissionStatus.denied;
    switch (permissionStatus) {
      case PermissionStatus.denied:
        condition = PermissionStatus.denied;
        break;
      case PermissionStatus.granted:
        condition = PermissionStatus.granted;
        break;
      case PermissionStatus.restricted:
        // 操作系统拒绝访问请求的特性。用户不能更改 仅ios
      condition=
          PermissionStatus.restricted;
        break;
      case PermissionStatus.limited:
        //  用户已授权此应用程序的有限访问。仅ios
      condition
          = PermissionStatus.limited;
        break;
      case PermissionStatus.permanentlyDenied:
        UIUtil.log("永久关闭了权限", permissionStatus);
        condition=
          PermissionStatus.permanentlyDenied;
        break;
      case PermissionStatus.provisional:
        condition = PermissionStatus.provisional;
        break;
    }
    UIUtil.log('有权限吗$permission', permissionStatus);
    return condition;
  }

  /// 获取手机系统版本号
  static Future<String> systemVersion() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();

    if (Platform.isIOS) {
      IosDeviceInfo version = await deviceInfo.iosInfo;
      List<String> list=version.systemVersion.split('.');
      return '${list[0]}.${list[1]}';
    } else if (Platform.isAndroid) {
      AndroidDeviceInfo info = await deviceInfo.androidInfo;
      String release = info.version.release;
      UIUtil.log("系统信息$release", release.split('.').first);
      return release.split('.').first;
    } else {
      return '未知设备';
    }
  }

  ///获取设备品牌信息
  static Future<String> deviceBrandInfo() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
      return androidDeviceInfo.brand;
    } else {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      return iosDeviceInfo.name;
    }
  }

  ///获取设备信息
  static Future<DeviceDetails> deviceSystemInfo() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      AndroidDeviceInfo androidDeviceInfo = await deviceInfo.androidInfo;
      return DeviceDetails(androidInfo: androidDeviceInfo);
    } else {
      IosDeviceInfo iosDeviceInfo = await deviceInfo.iosInfo;
      return DeviceDetails(iosInfo: iosDeviceInfo);
    }
  }

  /// App包信息
  static Future<PackageInfo> packageInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    return packageInfo;
  }

  /// 震动
  static vibrator({int duration = 50,pattern=const [10, 2], intensities=const[225]}) async {
    // 目标是否有震动能力
    if (await Vibration.hasVibrator()) {
      // Vibration.vibrate(duration: 1000,amplitude: 228);
      // UIUtil.log("震动模式设置1", 100);
    } else {
      UIUtil.log("当前设备不支持震动","");
    }

    // 检查目标设备是否能够控制Android 8.0 Oreo中引入的振动振幅-所有早期API级别的false。
    if (await Vibration.hasAmplitudeControl()) {
      // UIUtil.log("震动模式设置2", 100);
    }
    // 检查设备是否能够以自定义持续时间、模式或强度振动。即使设备没有振动器也可能返回true
    if (await Vibration.hasCustomVibrationsSupport()) {
      if (Platform.isAndroid) {
        Vibration.vibrate(duration: duration, amplitude: 1);
      } else {
        Vibration.vibrate(pattern: pattern, intensities: intensities);
      }
    } else {
      Vibration.vibrate();
      await Future.delayed(Duration(milliseconds: duration));
      Vibration.vibrate();
      UIUtil.log("自定义震动2下", 10);
    }
  }
}

class DeviceDetails {
  AndroidDeviceInfo? androidInfo;
  IosDeviceInfo? iosInfo;

  DeviceDetails({this.androidInfo, this.iosInfo});
}